package com.siam.package_promotion.mq_consumer;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.util.JsonUtils;
import com.siam.package_mall.entity.PointsMallOrder;
import com.siam.package_mall.entity.PointsMallOrderDetail;
import com.siam.package_mall.feign.PointsMallOrderFeignApi;
import com.siam.package_promotion.entity.DeductionMemberRelation;
import com.siam.package_promotion.service.DeductionMemberRelationService;

import lombok.SneakyThrows;

/**
 * 订单支付成功
 */
@Component
@RocketMQMessageListener(consumerGroup = "orderPaySuccessConsumerGroup", topic = "TO_PAY_SUCCESS")
public class OrderPaySuccessConsumer implements RocketMQListener<MessageExt> {
	
	private static final Logger logger = LoggerFactory.getLogger(OrderPaySuccessConsumer.class);
	
	@Autowired
	private DeductionMemberRelationService memberRelationService;
	@Autowired
	private PointsMallOrderFeignApi feignApi;
	
	@SneakyThrows
	@Override
	public void onMessage(MessageExt message) {
		switch (message.getTags()) {
		case Quantity.INT_6 + "":
			PointsMallOrder mallOrder =  (PointsMallOrder) JsonUtils.toObject(new String(message.getBody()), PointsMallOrder.class);
			BasicResult<PointsMallOrder> orderResult = feignApi.selectByOrderNo(mallOrder.getOrderNo());
			if(!orderResult.getSuccess() || orderResult.getData() == null) {
				logger.error("处理抵扣劵购买获取订单信息失败,订单号: {} ",mallOrder.getOrderNo());
				return ;
			}
			BasicResult<List<PointsMallOrderDetail>> basicResult = feignApi.selectOrderDetailByOrderId(mallOrder.getId());
			if(!basicResult.getSuccess() || basicResult.getData() == null) {
				logger.error("处理抵扣劵购买获取订单详细信息失败,订单号: {} ",mallOrder.getOrderNo());
				return ;
			}
			int count = 0;
			for (PointsMallOrderDetail detail : basicResult.getData()) {
				if(detail.getGoodsType() == Quantity.INT_2) {
					int countDeduction = memberRelationService.countByOrderId(orderResult.getData().getId().intValue());
					if(countDeduction>0) {
						logger.info("处理抵扣劵购买已发放,订单号: {}",mallOrder.getOrderNo());
						continue;
					}
						// 抵扣劵发货
						for (int i = 0; i < detail.getNumber(); i++) {
							Object discountValue =  JsonUtils.toMap(detail.getExtJson()).get("discountValue");
							DeductionMemberRelation memberRelation = new DeductionMemberRelation();
							memberRelation.setName(detail.getGoodsName());
							memberRelation.setDeductionAmount(new BigDecimal(discountValue.toString()));
							memberRelation.setCostAmount(detail.getPrice());
							memberRelation.setGoodsId(detail.getGoodsId().intValue());
							memberRelation.setOrderId(orderResult.getData().getId().intValue());
							memberRelation.setMemberId(orderResult.getData().getMemberId());
							memberRelation.setIsUsed(0);
							memberRelation.setIsValid(1);
							memberRelationService.insertSelective(memberRelation);
							count++;
						}
				}
			}
			logger.info("优惠卷插入数据:{} 订单商品数量:{}",count,orderResult.getData().getGoodsTotalQuantity());
			if(count == orderResult.getData().getGoodsTotalQuantity()) {
				// 把订单结束掉
				feignApi.updateFinishByOrderId(orderResult.getData().getId().intValue());
			}
			break;
		}
		
	}

}
